package com.taomeng.service.impl;

import com.taomeng.dao.StudentDao;
import com.taomeng.entity.PageBean;
import com.taomeng.entity.Student;
import com.taomeng.entity.User;
import com.taomeng.service.StudentService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;

    @Override
    public Student findByStudentNum(String studentNum) {
        return studentDao.findByStudentNum(studentNum);
    }

    public Map<String, Object> findAllInfoByStudentNum(String studentNum) {

        Map<String, Object> map = studentDao.findAllInfoByStudentNum(studentNum);
        return map;

    }

    @Override
    public PageBean<Student> findByPage(int page, int limit, String key) {
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipal(); //获取当前用户
        /*获取当前用户的角色
         * 班主任:只能看自己管理的班级学生信息
         * 辅导员：能看本学院的学生信息
         * 超管： 可以看全部的学生信息
         */

        if (subject.hasRole("admin")||subject.hasRole("headadmin")) {
            return studentDao.findByPage(page, limit, key, null, null);
        } else if (subject.hasRole("fudaoyuan") || subject.hasRole("deptadmin")) {
            return studentDao.findByPage(page, limit, key, principal.getDepartmentId(), null);
        } else if (subject.hasRole("banzhuren")) {
            return studentDao.findByPage(page, limit, key, null, principal.getId());
        }
        return null;
    }

    @Override
    public boolean add(Student student) {
        return studentDao.add(student) == 1;
    }

    @Override
    public boolean delStudentById(int id) {
        return studentDao.delStudentById(id) == 1;
    }

    @Override
    public boolean delBatchAll(String ids) {
        return studentDao.delBatchAll(ids);
    }

    @Override
    public boolean updateStudent(Student student) {
        return studentDao.updateStudent(student) == 1;
    }
}
